package com.ciccwm.auth.strategy;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 认证策略工厂
 */
@Component
@RequiredArgsConstructor
public class AuthenticationStrategyFactory {
    
    private final Map<String, AuthenticationStrategy> strategyMap;
    
    public AuthenticationStrategyFactory(List<AuthenticationStrategy> strategies) {
        this.strategyMap = strategies.stream()
            .collect(Collectors.toMap(
                AuthenticationStrategy::getType,
                Function.identity()
            ));
    }
    
    /**
     * 获取认证策略
     *
     * @param type 策略类型
     * @return 认证策略
     */
    public AuthenticationStrategy getStrategy(String type) {
        AuthenticationStrategy strategy = strategyMap.get(type);
        if (strategy == null) {
            throw new IllegalArgumentException("Unsupported authentication type: " + type);
        }
        return strategy;
    }
    
    /**
     * 获取所有支持的认证类型
     *
     * @return 认证类型列表
     */
    public List<String> getSupportedTypes() {
        return List.copyOf(strategyMap.keySet());
    }
} 
